package com.tbsoft.ehr.action.staffFile;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.model.Contract;
import com.tbsoft.ehr.model.StaffInfo;
import com.tbsoft.ehr.service.contract.ContractService;
import com.tbsoft.ehr.service.staffile.StaffFileService;

public class ContractAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Log log = LogFactory.getLog(ContractAction.class);
	//档案对象
	private StaffInfo staff;
	//合同信息
	private Contract contract;
	//存放合同列表
	private List<Contract> contractLst;
	
	//查询合同条件参数
	private String continueMonth;
	private String staffName;
	
	private ContractService contractService;
	private StaffFileService staffFileService;
	
	public StaffInfo getStaff() {
		return staff;
	}

	public void setStaff(StaffInfo staff) {
		this.staff = staff;
	}

	public Contract getContract() {
		return contract;
	}

	public void setContract(Contract contract) {
		this.contract = contract;
	}	

	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public String getContinueMonth() {
		return continueMonth;
	}

	public void setContinueMonth(String continueMonth) {
		this.continueMonth = continueMonth;
	}

	public List<Contract> getContractLst() {
		return contractLst;
	}

	public void setContractLst(List<Contract> contractLst) {
		this.contractLst = contractLst;
	}

	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}

	public StaffFileService getStaffFileService() {
		return staffFileService;
	}

	public void setStaffFileService(StaffFileService staffFileService) {
		this.staffFileService = staffFileService;
	}

	public String add() {
		if(this.staff!=null&&this.staff.getStaffId()!=null) {
			this.staff = this.staffFileService.getStaffFileById(this.staff.getStaffId());
			this.contractLst = this.contractService.getContractByStaff(this.staff);
			return "add";
		} 
		return SUCCESS;
	}
	
	public String save() {
		this.contract.setStaff(this.staff);
		this.contractService.addContract(this.contract);
		setRel("staffFile_browse");
		return SUCCESS;
	}
	
	public String query() {
		//用于分页
		int pageNum = this.getPageNum() > 0 ? this.getPageNum() - 1 : 0;
		int startIndex = pageNum * getNumPerPage();		
				
		String endTimeFromStr=null;
		String endTimeToStr = null;
		if(this.continueMonth!=null && this.continueMonth.length()>0) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.MONTH, Integer.parseInt(this.continueMonth)-1);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			endTimeFromStr = sdf.format(calendar.getTime());
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			endTimeToStr = sdf.format(calendar.getTime());
		}
				
		List<StaffInfo> staffLst = null;
		if(this.staffName!=null&&this.staffName.length()>0) {
			staffLst = this.staffFileService.getStaffFileByStaffName(this.staffName);
			//输入的姓名没有取到对应的档案，就不去查询合同了
			if(staffLst.size()==0){
				return "browse";
			}
		}
		
		int count = this.contractService.getContractCount(staffLst,endTimeFromStr,endTimeToStr);
		log.info("该条件下获得数据总条数为："+count);
		setTotalCount(count);
		setPageNumShown((getNumPerPage()+count)/getNumPerPage());
		this.contractLst = this.contractService.getContractByCondton(staffLst, endTimeFromStr, endTimeToStr,startIndex,getNumPerPage());
		return "browse";
	}
}
